<?php
namespace Admin\Controller;

class CourseApplyController extends BaseController
{
    public function indexAction()
    {
        $course_id = I('course_id', 0, 'intval');
        $CourseApply = M('CourseApply');
        $condition = array();
        if ($course_id) {
            $condition['course_id'] = $course_id;
        }
        $count = $CourseApply->where($condition)->count();
        $Page = new \Think\Page($count, 50);
        $show = $Page->show();
        $list = $CourseApply->where($condition)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach($list as $key => $item) {
            $list[$key]['course_info'] = M('Course')->field('name')->find($item['course_id']);
        }
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->display();
    }

    public function viewAction()
    {
        $CourseApply = D('CourseApply');
        if (IS_POST) {
            $data = $CourseApply->create($_POST['CourseApply']);
            if (false !== $data) {
                if (false !== $CourseApply->save()) {
                    $this->success('更新成功', U('CourseApply/index'));
                } else {
                    $this->error('更新失败（失败原因：' . $CourseApply->getError() . '）');
                }
            } else {
                $this->error($CourseApply->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $info = $CourseApply->find($id);
            $info['course_info'] = M('Course')->field('name')->find($info['course_id']);
            $this->assign('info', $info);
            $this->display();
        }
    }
}